package cn.pli.bike.model;

import com.tencent.mm.opensdk.modelpay.PayReq;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;


import cn.pli.bike.App;
import cn.pli.bike.bean.AnnouncementBean;
import cn.pli.bike.bean.BadErrBean;
import cn.pli.bike.bean.BikeCoordinateBean;
import cn.pli.bike.bean.CheckUpdateBean;
import cn.pli.bike.bean.CouponBean;
import cn.pli.bike.bean.CreditRecord;
import cn.pli.bike.bean.DepositBean;
import cn.pli.bike.bean.ExpensesRecordBean;
import cn.pli.bike.bean.HttpResult;
import cn.pli.bike.bean.IllegalPakingBean;
import cn.pli.bike.bean.LockBean;
import cn.pli.bike.bean.OpenLockBean;
import cn.pli.bike.bean.OpenOrderBean;
import cn.pli.bike.bean.OrderinfoBean;
import cn.pli.bike.bean.PrepaidRecord;
import cn.pli.bike.bean.ProblemBean;
import cn.pli.bike.bean.SettingBean;
import cn.pli.bike.bean.ShareBean;
import cn.pli.bike.bean.UserBean;
import cn.pli.bike.model.InterfaceService.UserCreateService;
import cn.pli.bike.model.http.ChunkingConverterFactory;
import cn.pli.bike.model.http.FileRequestBody;
import cn.pli.bike.model.http.RetrofitCallback;
import cn.pli.bike.model.progress.ProgressListener;
import cn.pli.bike.model.subscribers.ProgramSubscriber;
import cn.pli.bike.url.API;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.POST;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * @author zl
 * @date 2017/9/14
 */

public class UserModel {
    private static String DEFAULT = "default";

    public static void getStartPage(ProgramSubscriber<HttpResult<List<String>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getStartPage()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 获取手机验证码
     *
     * @param mobile
     * @return
     */
    public static void postVerifyCode(ProgramSubscriber<HttpResult<String>> programSubscriber, String mobile) {
        RxService.createApi(UserCreateService.class)
                .postVerifyCode(mobile)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 获取韩国验证码
     *
     * @param mobile
     * @return
     */
    public static void postKrVerifyCode(ProgramSubscriber<HttpResult<String>> programSubscriber, String mobile) {
        RxService.createApi(UserCreateService.class)
                .postKrVerifyCode(mobile)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 注册或登录
     *
     * @return
     */
    public static void postSignupOrLogin(ProgramSubscriber<UserBean> programSubscriber, String mobile, String verifyCode) {
        RxService.createApi(UserCreateService.class)
                .postSignupOrLogin(mobile, verifyCode, App.getApp().amapLocation.getLongitude() + "", "" + App.getApp().amapLocation.getLatitude())
                .map(new Func1<HttpResult<UserBean>, UserBean>() {

                    @Override
                    public UserBean call(HttpResult<UserBean> userBeanHttpResult) {
                        return userBeanHttpResult.getData();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * touken登录
     *
     * @return
     */
    public static void postloginByToken(ProgramSubscriber<HttpResult<UserBean>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .postloginByToken(DEFAULT)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 上传头像
     *
     * @param picture
     * @return
     */
   /* @POST("khBike.do?uploadImage")
    Observable<HttpResult<String>> uploadImage(@Body RequestBody picture);*/

    /**
     * 修改昵称
     *
     * @param
     * @return
     */
    public static void editNickname(ProgramSubscriber<HttpResult<String>> programSubscriber, String nickName) {
        RxService.createApi(UserCreateService.class)
                .editNickname(nickName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 打开故障上报页面,查询故障类型
     *
     * @param
     * @return
     */
    public static void goFaultReport(ProgramSubscriber<HttpResult<List<BadErrBean>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .goFaultReport("goFaultReport")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 故障提交
     *
     * @param
     * @return
     */
    public static void faultReport(ProgramSubscriber<HttpResult<String>> programSubscriber, Map<String, String> map) {
        RxService.createApi(UserCreateService.class)
                .faultReport(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 常见问题
     *
     * @param
     * @return
     */
    public static void commonProblem(ProgramSubscriber<HttpResult<List<ProblemBean>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .commonProblem()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 查询违停类型
     *
     * @param
     * @return
     */
    public static void goIllegalParking(ProgramSubscriber<HttpResult<List<BadErrBean>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .goIllegalParking(DEFAULT)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 违停上报提交
     *
     * @param
     * @return
     */
    public static void illegalParking(ProgramSubscriber<HttpResult<String>> programSubscriber, Map<String, String> map) {
        RxService.createApi(UserCreateService.class)
                .illegalParking(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 获取
     *
     * @param
     * @return
     */
    public static void getPrepaidRecord(ProgramSubscriber<HttpResult<List<PrepaidRecord>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getPrepaidRecord(DEFAULT)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 获取用户消费记录（我的行程）
     *
     * @param
     * @return
     */
    public static void getExpensesRecord(ProgramSubscriber<HttpResult<List<ExpensesRecordBean>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getExpensesRecord(DEFAULT)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 获取用户信用积分记录
     *
     * @param
     * @return
     */
    public static void getCreditRecord(ProgramSubscriber<HttpResult<List<CreditRecord>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getCreditRecord(DEFAULT)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 获取系统公告列表
     *
     * @param
     * @return
     */
    public static void getAnnouncement(ProgramSubscriber<HttpResult<List<AnnouncementBean>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getAnnouncement()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 上传 图片  公用
     *
     * @return
     */
    public static void uploadImage(String path, ProgramSubscriber<HttpResult<String>> programSubscriber, final ProgressListener progressListener) {
        File file = new File(path);
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        FileRequestBody fileRequestBody = new FileRequestBody(FileRequestBody.create(MediaType.parse("image/png/jpg; charset=utf-8"), file), new RetrofitCallback() {
            @Override
            public void onSuccess(Call call, Response response) {
            }

            @Override
            public void onFailure(Call call, Throwable t) {
            }

            @Override
            public void onLoading(long total, long progress) {
                super.onLoading(total, progress);
                progressListener.onProgress(total, progress);
            }
        });
        //     builder.addFormDataPart("picture", file.getName(), RequestBody.create(MediaType.parse("image/png/jpg; charset=utf-8"), file));
        builder.addFormDataPart("Content-Type", file.getName(), fileRequestBody);
        MultipartBody body = builder.build();
        RxService.createApi(UserCreateService.class, API.URL.BASE_URL, new ChunkingConverterFactory(body, progressListener))
                .uploadImage(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 上传 图片  公用
     *
     * @return
     */
    public static void uploadUserPhoto(String path, final ProgramSubscriber<HttpResult<String>> programSubscriber, final ProgressListener progressListener) {
        File file = new File(path);
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        FileRequestBody fileRequestBody = new FileRequestBody(FileRequestBody.create(MediaType.parse("image/png/jpg; charset=utf-8"), file), new RetrofitCallback() {
            @Override
            public void onSuccess(Call call, Response response) {
            }

            @Override
            public void onFailure(Call call, Throwable t) {
            }

            @Override
            public void onLoading(long total, long progress) {
                super.onLoading(total, progress);
                if (progressListener != null) {
                    progressListener.onProgress(total, progress);
                }
            }
        });
        builder.addFormDataPart("Content-Type", file.getName(), fileRequestBody);
        MultipartBody body = builder.build();
        RxService.createApi(UserCreateService.class, API.URL.BASE_URL, new ChunkingConverterFactory(body, progressListener))
                .uploadUserPhoto(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 支付宝支付
     *
     * @param consumptionType 0,押金 1，账户充值，2购买用车券
     * @return
     */
    public static void aliPay(ProgramSubscriber<HttpResult<OrderinfoBean>> programSubscriber, double cashnum, String consumptionType) {
        RxService.createApi(UserCreateService.class)
                .aliPay(cashnum, consumptionType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 微信支付
     *
     * @param consumptionType 0,押金 1，账户充值，2购买用车券
     * @return
     */
    public static void wxPay(ProgramSubscriber<HttpResult<String>> programSubscriber, double cashnum, String consumptionType) {
        RxService.createApi(UserCreateService.class)
                .wxPay(cashnum, consumptionType)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 根据坐标获取周围单车
     *
     * @param
     * @return
     */
    public static void findBikeByCoordinate(ProgramSubscriber<HttpResult<List<BikeCoordinateBean>>> programSubscriber, double longitude, double latitude) {
        Map<String, Double> map = new HashMap<>();
        map.put("longitude", longitude);
        map.put("latitude", latitude);

        RxService.createApi(UserCreateService.class)
                .findBikeByCoordinate(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 根据坐标获取周围单车
     *
     * @param
     * @return
     */
    public static void openLockScan(ProgramSubscriber<HttpResult<OpenLockBean>> programSubscriber, String locknum, String longitude, String latitude) {
        Map<String, String> map = new HashMap<>();
        map.put("accuracy", longitude);
        map.put("latitude", latitude);
        map.put("locknum", locknum);

        RxService.createApi(UserCreateService.class)
                .openLockScan(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 关锁
     *
     * @return
     */
    public static void closeLock(ProgramSubscriber<HttpResult<ExpensesRecordBean>> programSubscriber, String orderId, String endLongitude, String endLatitude) {
        RxService.createApi(UserCreateService.class)
                .closeLock(orderId, endLongitude, endLatitude)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 关锁
     *
     * @return
     */
    public static void getDeposit(ProgramSubscriber<HttpResult<DepositBean>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getDeposit()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 关锁
     *
     * @param
     * @return
     */
    public static void cardCheckIDCard(ProgramSubscriber<HttpResult<String>> programSubscriber, String name, String id) {
        RxService.createApi(UserCreateService.class)
                .cardCheckIDCard(name, id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 关锁
     *
     * @param
     * @return
     */
    public static void refundMoney(ProgramSubscriber<HttpResult<String>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .refundMoney(DEFAULT)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    /**
     * 关锁
     *
     * @param
     * @return
     */
    public static void getAppSetting(ProgramSubscriber<HttpResult<List<SettingBean>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .appSettings()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 关锁
     *
     * @param
     * @return
     */
    public static void checkUpdate(ProgramSubscriber<HttpResult<CheckUpdateBean>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .checkUpdate()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


    public static void selectOpenOrder(ProgramSubscriber<HttpResult<OpenOrderBean>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .selectOpenOrder()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 获取用户所有优惠券
     *
     * @param
     * @return
     */
    public static void getCoupon(ProgramSubscriber<HttpResult<List<CouponBean>>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getCoupon()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 查询是否有未结束订单
     *
     * @param
     * @return
     */
    public static void getShareAttribute(ProgramSubscriber<HttpResult<ShareBean>> programSubscriber) {
        RxService.createApi(UserCreateService.class)
                .getShareAttribute()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    /**
     * 查询是否有未结束订单
     *
     * @param
     * @return
     */
    public static void getLockByLocknum(ProgramSubscriber<HttpResult<LockBean>> programSubscriber, String lockNum) {
        RxService.createApi(UserCreateService.class)
                .getLockByLocknum(lockNum)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    public static void blueOpenNotice(ProgramSubscriber<HttpResult<List<String>>> programSubscriber, String lockNum, String beginLongitude, String beginLatitude) {
        RxService.createApi(UserCreateService.class)
                .blueOpenNotice(lockNum, beginLongitude, beginLatitude)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }

    public static void notice(ProgramSubscriber<HttpResult<String>> programSubscriber, String imei) {
        RxService.createApi(UserCreateService.class)
                .notice(imei, "ffffffff", "ffffffff")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(programSubscriber);
    }


}
